﻿#include "MyPushButton.h"
#include <QMouseEvent>
#include <QPointF>
#include <QCoreApplication>

MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
{

}

void MyPushButton::mousePressEvent(QMouseEvent *event)
{
    if(this->parentWidget())
    {
        //将Button点击的坐标转换为父Widget的坐标后然后新建一个事件发送给父Widget
        QPointF point = mapTo(this->parentWidget(), event->pos());
        //注意这里的 new 不会造成内存泄漏，详见QT帮助文档 postEvent 的解释
        QMouseEvent *event_ = new QMouseEvent(event->type(), point, event->button(), event->buttons(), event->modifiers());
        QCoreApplication::postEvent(this->parentWidget(), event_);
    }
    QPushButton::mousePressEvent(event);
}
